

#import "PURecordVC.h"
#import "PUCityListModel.h"
#import "PUModel.h"
#import "PUPublishVC.h"
#import "PURecordCell.h"
#import "PUXuanCaiDetailsVC.h"
@interface PURecordVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)NSMutableArray *dizhiData;

@end

@implementation PURecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor clearColor];
    self.title=@"炫菜记录";
    self.dizhiData=[NSMutableArray array];
    
    _tableView = [[UITableView alloc] init];
    _tableView.backgroundColor=[UIColor clearColor];
    //解决scrollToRowAtIndexPath：滚动位置偏差问题
    _tableView.estimatedRowHeight = 0;
    _tableView.estimatedSectionFooterHeight = 0;
    _tableView.estimatedSectionHeaderHeight = 0;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//设置分隔线样式
    _tableView.bounces = YES;
    _tableView.delegate=self;
    _tableView.dataSource=self;
    [self.view addSubview:self.tableView];
    self.edgesForExtendedLayout = UIRectEdgeTop;
//    _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;


    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset = 0;
    }];
    
    self.tableView.emptyDataSetSource = self.tableView;
    self.tableView.emptyDataSetDelegate= self.tableView;
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadData];
    }];
    
//    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        [weakSelf loadMoreRecommendData];
//    }];
    
    self.tableView.mj_footer.ignoredScrollViewContentInsetBottom = iPhoneXs ? 34 : 0;
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer.hidden = YES;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(relodGHJLNotification) name:@"pushGHJLNotification" object:nil];
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updataData) name:@"loginSuccessUpdataNotification" object:nil];//登录成功---刷新列表
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scroVC) name:@"showLoginNotification" object:nil];//退出登录成功，列表有数据时滚动到第一个
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(daShanSuccess:) name:@"chatAccostSuccessNotification" object:nil];//搭讪成功
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(behaviorBlackSuccess:) name:@"CPBehaviorBlackNotification" object:nil];//用户详情页拉黑
}
-(void)relodGHJLNotification{
    [self loadData];
}

-(void)buttonAction{
    PUPublishVC *vc=[[PUPublishVC alloc]init];
    vc.type=1;
    vc.isGHEnter=1;
    vc.huaTiStr=@"";
//    vc.modalPresentationStyle=0;
//    [self presentViewController:vc animated:YES completion:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)scroVC{
    if (self.dizhiData.count>0) {
        
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PURecordCell *cell = (PURecordCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.rowHeight;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dizhiData.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    PUModel *model=self.dizhiData[indexPath.row];
    static NSString *cellIdentifier1 = @"XLGuanHuaRecordCell";//定义一个可重用标识
    PURecordCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];//从重用队列里获取可重用的cell
    if (cell==nil)
    {
        //如果不存在，创建一个可重用cell
        cell =[[PURecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier1] ;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.backgroundColor=[UIColor clearColor];
    cell.model=model;
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    return;
    
    PUModel *model=self.dizhiData[indexPath.row];
    PUXuanCaiDetailsVC *vc=[[PUXuanCaiDetailsVC alloc]init];
    vc.isSJEnter=1;
    vc.isSelf=1;
    vc.model=model;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - GKPageListViewDelegate
- (UIView *)listView {
    return self.view;
}

- (UIScrollView *)listScrollView {
    return self.tableView;
}

-(void)loadData{
    [self loadArchiveData];
    [self addEmptyData];
}
-(void)loadArchiveData{
    NSMutableArray *data=[NSMutableArray array];
    NSString *library=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
    NSString *strLib = [library stringByAppendingPathComponent:@"archive"];
    BOOL directory = YES;
    if (![[NSFileManager defaultManager] fileExistsAtPath:strLib isDirectory:&directory]){
        [[NSFileManager defaultManager] createDirectoryAtPath:strLib withIntermediateDirectories:NO attributes:nil error:nil];
    }
    //从归档中读出
    NSString *arrayArchive =[strLib stringByAppendingPathComponent:[NSString stringWithFormat:@"dongtaiDataArchive%@",[Config getID]]];//用首页唯一标示id创建文件名
    NSArray *categoryAry = [NSKeyedUnarchiver unarchiveObjectWithFile:arrayArchive];
    if (categoryAry&&[categoryAry count])
    {
        data=[NSMutableArray arrayWithArray:categoryAry];
        self.dizhiData= [PUModel mj_objectArrayWithKeyValuesArray:data];
    }
    else
    {
//        [self getRequest];//get请求界面
    }
}

-(void)addEmptyData{
    [self.tableView.mj_header endRefreshing];
    self.tableView.configuer.emptyType =  ( self.dizhiData.count == 0) ? DznEmptyDataTypeSuccess : DznEmptyDataTypeDefault;
//        self.tableView.configuer.emptyType =  DznEmptyDataTypeSuccess;
    [self.tableView reloadData];
}

-(void)showSVProgressHUDStr:(NSString *)str{
    if ([str isKindOfClass:[NSNull class]]||str==nil||[str isEqualToString:@""]) {
        return;
    }
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD showImage:[UIImage imageNamed:@""] status:str];
}
/**
 响应链的方法
 */
- (UIViewController *)viewController {
    for (UIView* next = [self.view superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

@end
